package com.atme.micromall.member.controller;

import com.aliyuncs.exceptions.ClientException;
import com.atme.micromall.member.bean.Members;
import com.atme.micromall.member.dto.CommonResult;
import com.atme.micromall.member.dto.MemberParam;
import com.atme.micromall.member.note.PassToken;
import com.atme.micromall.member.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member")
@Api(tags = "会员服务")
public class MemberController {

    private final static Logger logger = LoggerFactory.getLogger(MemberController.class);

    @Autowired
    private MemberService memberService;


    /**
     *
     * @param userNameOrPhoneNumber
     * @param password
     * @param phoneNumber
     * @param smsCode  验证码
     * @param login   用户名/手机号+密码登录：login=1；手机验证码登录：login=2
     * @return
     */
    @PassToken
    @ApiOperation("登录")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public CommonResult login(@RequestParam(value = "userName",required = false) String userNameOrPhoneNumber,
                              @RequestParam(value = "password",required = false) String password,
                              @RequestParam(value = "phoneNumber",required = false) String phoneNumber,
                              @RequestParam(value = "smsCode",required = false) String smsCode,
                              @RequestParam("login") Integer login){

        memberService.login(userNameOrPhoneNumber,password,phoneNumber,smsCode,login);
        return null;
    }


    /**
     * 注册
     * @param phoneNumber
     * @param smsCode
     * @return
     */
    @ApiOperation("注册")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult create(@RequestParam(value = "phoneNumber") String phoneNumber,
                               @RequestParam(value = "smsCode") String smsCode) {

        int count = memberService.create(phoneNumber, smsCode);
        if (count > 0) {
            return CommonResult.success(count);
        } else if (count == -1) {
            return CommonResult.failed("该手机号已注册");
        }
        return CommonResult.failed("验证码错误");
    }


    /**
     * 发送短信验证码
     * @param phoneNumber
     * @return
     */
    @ApiOperation("发送短信验证码")
    @RequestMapping(value = "/sms",method = RequestMethod.GET)
    public CommonResult setSms(@RequestParam("phoneNumber") String phoneNumber) throws ClientException {

        memberService.setSms(phoneNumber);

        return CommonResult.success("发送成功");

    }


    /**
     * 查询会员个人信息
     * @param id
     * @return
     */
    @ApiOperation("查询会员个人信息")
    @RequestMapping(value = "/list/{id}",method = RequestMethod.GET)
    public CommonResult getList(@PathVariable("id") Integer id){
        Members members=memberService.getMemberById(id);
        if (members==null){
            return CommonResult.failed();
        }
        return CommonResult.success(members);
    }


    /**
     * 修改会员个人信息
     * @param id
     * @param memberParam
     * @return
     */
    @ApiOperation("修改会员个人信息")
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public CommonResult update(@PathVariable("id") Integer id,
                               @RequestBody MemberParam memberParam){
        int count = memberService.update(id,memberParam);
        logger.info("update");
        logger.error("update");
        if (count>0){
            return CommonResult.success(count);
        }else if(count==-1){
            return CommonResult.failed("用户名重复");
        }else {
            return CommonResult.failed();
        }
    }


    /**
     * 模拟修改积分
     * @param id
     * @param number
     * @return
     */
    @ApiOperation("模拟修改积分")
    @RequestMapping(value = "/updatePoints", method = RequestMethod.POST)
    public CommonResult updatePoints(@RequestParam(value = "id") Integer id,
                               @RequestParam(value = "number") Integer number) {

        int count = memberService.updatePoints(id,number);
        if (count==1){
            return CommonResult.success(count);
        }

        return CommonResult.failed();
    }


}
